define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'money/userbalance/index' + location.search,
                    add_url: 'money/userbalance/add',
                    edit_url: 'money/userbalance/edit',
                    del_url: 'money/userbalance/del',
                    multi_url: 'money/userbalance/multi',
                    import_url: 'money/userbalance/import',
                    table: 'user_balance',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.mobile', title: __('用户手机号')},
                        {
                            field: 'type',
                            title: __('类型'),
                            searchList: {"in": __('收入'), "out": __('支出')},
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'amount', title: __('金额'), operate: false},
                        {field: 'mark', title: __('备注'), operate: false},
                        {
                            field: 'create_time',
                            title: __('时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'shop.name', title: __('店铺'), operate: false},
                        {field: 'order_id', title: __('订单ID'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
